package com.udaos.modules.school.services;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;

import com.udaos.common.base.Service;
import com.udaos.models.school.Ud_school;
import com.udaos.models.sys.Sys_user;

/**
 * 驾校Service
 * 
 * @author mutaotao
 * @time 2016-11-08 18:11:35
 * 
 */
@IocBean(args = { "refer:dao" })
public class UdSchoolService extends Service<Ud_school> {
	public UdSchoolService(Dao dao) {
		super(dao);
	}

	public List<Ud_school> find() {
		Cnd cnd = Cnd.NEW();
		cnd.and("delFlag", "=", "0");
		cnd.and("state", "=", 0);
		return dao().query(Ud_school.class, cnd);
	}

	public List<Ud_school> findByRegionId(String region_id) {
		Cnd cnd=Cnd.NEW();
		cnd.and("region_id", "=", region_id);
		cnd.and("delFlag", "=", "0");
		cnd.and("state", "=", 0);
		return dao().query(Ud_school.class, cnd);
	}
	
	/**
	 * 加载数据
	 * @param length
	 * @param start
	 * @param draw
	 * @param name
	 * @return
	 */
	public Object init(int length, int start, int draw, String name) {
		String sqls="SELECT s.id AS id, s.name AS name,s.code AS code ,s.linkman AS linkman ,s.phone AS phone ,g.name AS region_name,s.is_branch AS is_branch ,s.join_time AS join_time,s.state AS state"
				  +" FROM ud_school s,ud_region g "
				  +" WHERE s.region_id=g.id AND s.delFlag=FALSE";
		if(!Strings.isBlank(name)){
			sqls+=" AND s.name LIKE '%"+name+"%'";
		}
	
		Sql sql =Sqls.create(sqls);
		return this.data(length, start, draw, sql, sql);
	}
}